﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.Preferences;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.ModuleData;
using Plumber.Accounting.Preferences;
using Plumber.Masters.ModuleData;
using Plumber.Sales.ModuleData;
using System.Data;

namespace Plumber.Accounting.Scrounger
{
    internal class CompanyScrounger : ApplicationLibrary.DBManager.Scrounger
    {
        private UserTabControl<TabLiquidationType> UtctabLT = null;
        private TabLiquidationType tabLT = null;
        private Masters.Forms.Company frm;

        public CompanyScrounger(iScrounger frm)
            : base(frm)
        {
            this.frm = (Masters.Forms.Company)frm;
        }

        public override void InitializerComponent()
        {
            UtctabLT = new UserTabControl<TabLiquidationType>(Properties.Resources.TD_LiquidationType);
            UtctabLT.Name = "tpgLT";

            frm.masterTabControl.Controls.Add(UtctabLT);
            tabLT = UtctabLT.UserControl;

            base.InitializerComponent();
        }

        public override void OnAttachData(DBManager dbManager)
        {
            dbManager.AddSlave(PL_LiquidationType.Name, true);
            dbManager.AddRelation("LiquidationTypeSlave", PL_Company.Code, PL_LiquidationType.Company, false);
            tabLT.OnAttachData(dbManager);

            base.OnAttachData(dbManager);
        }

        public override void OnPrepareAuxData()
        {
            tabLT.AutoResizeColumns();
            base.OnPrepareAuxData();
        }

        public override void OnBindData()
        {
            tabLT.OnBindData(frm);
            base.OnBindData();
        }

        public override string CreateSlaveQuery(string name)
        {
            QueryBuilder qb = new QueryBuilder();
            if (name == PL_LiquidationType.Name)
            {
                qb.SetQualified = true;
                qb.AddSelect();
                qb.AddFrom(PL_LiquidationType.Name);
                qb.AddCompare(PL_LiquidationType.Company, "@p4");
                qb.AddOrder(PL_LiquidationType.Year);
            }
            return qb.Query;
        }

        public override List<SqlABParameter> CreateSlaveParam(string name)
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();
            if (name == PL_LiquidationType.Name)
            {
                SqlABParameter p1 = new SqlABParameter("@p4", PL_LiquidationType.Company);
                p1.Value = "0";
                PList.Add(p1);
            }
            return PList;
        }

        public override void SetParameters(DBManager dbManager, IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == PL_LiquidationType.Name)
            {
                collection.Parameter[0].Value = key[0];
            }
            base.SetParameters(dbManager, key, collection);
        }

        public override void RowUpdating(DBManager dbManager, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                if (e.Row.Table.ToString() == PL_LiquidationType.Name)
                    e.Row[PL_LiquidationType.Company.Name] = 1;
            }
        }
    }
}